<?php
class Dmf_Service_Infodb
{
    private $_key = '79f659dfecc7126f47ec51eeec5cf455311f727141edee2e70d36cb5ca014e64';
    
    private $_http;
    
    private $_infodb_api = 'http://api.ipinfodb.com/v3/ip-city/';
    
    public function __construct()
    {
        $this->_http = new Dmf_Http();
    }
    
    public function getData( $ip )
    {
        $get = $this->_http->get( $this->_infodb_api . '?key=' . $this->_key . '&ip=' . $ip . '&format=json' );
        
        if ( ! $get->ok() )
        {
            throw new Exception( 'Something went wrong with connection to infodb: <small>' . $get->getError() . '</small>' );
        }
        
        $array = $get->getJson();
        
        if ( strtolower( $array['statusCode'] ) != 'ok' )
        {
            throw new Dmf_Exception_InfodbAPI( 'Something went wrong with infoDB api.' );
        }
        
        //Add some simple indexes
        $array['country'] = $array['countryName'];
        $array['region'] = $array['regionName'];
        $array['city'] = $array['cityName'];
        $array['zipcode'] = $array['zipCode'];
        
        return Dmf_Stdclass::fromArray( $array );
    }
}